package com.seckill.product.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.seckill.product.entity.OrderSurfaceEntity;
import com.seckill.product.service.OrderSurfaceService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author zhuxz
 * @email sunlightcs@gmail.com
 * @date 2022-06-19 09:38:15
 */
@RestController
@RequestMapping("product/ordersurface")
public class OrderSurfaceController {
    @Autowired
    private OrderSurfaceService orderSurfaceService;

    @Autowired
    private HttpServletResponse response;

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderSurfaceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")

    public R info(@PathVariable("id") Integer id){
		OrderSurfaceEntity orderSurface = orderSurfaceService.getById(id);

        return R.ok().put("orderSurface", orderSurface);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody OrderSurfaceEntity orderSurface){
		orderSurfaceService.save(orderSurface);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody OrderSurfaceEntity orderSurface){
		orderSurfaceService.updateById(orderSurface);

        return R.ok();
    }

    //导人excel表格
    @RequestMapping("/dexcel")
    public R dexcel(MultipartFile fileName) throws IOException {
        ArrayList<OrderSurfaceEntity> list = new ArrayList<>();
        EasyExcel.read(fileName.getInputStream(), OrderSurfaceEntity.class, new AnalysisEventListener<OrderSurfaceEntity>() {
                    //重写子类方法
                    @Override
                    public void invoke(OrderSurfaceEntity question1, AnalysisContext analysisContext) {
                        String sheetName = analysisContext.readSheetHolder().getSheetName();
                        list.add(question1);
                    }
                    //重写子类方法
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                    }

                    @Override
                    public void invokeHeadMap(Map headMap, AnalysisContext context) {
                        System.out.println(headMap);
                    }
                }
        ).doReadAll();

        System.out.println(list);
        return R.ok().put("page", list);
    }

    //excel导出
    @RequestMapping("/excelOut")
    public void getExcel(HttpServletRequest request, HttpServletResponse response) {
        //获取数据
        List<OrderSurfaceEntity> list = orderSurfaceService.list();
        ArrayList<OrderSurfaceEntity> arr = new ArrayList<>();
        for (OrderSurfaceEntity physicianEntity : list) {
            arr.add(JSONObject.parseObject(JSON.toJSONString(physicianEntity),new TypeReference<OrderSurfaceEntity>(){}));
        }
        System.out.println(arr);
        try {
            long l = System.currentTimeMillis();
            String fileName = String.valueOf(l);
            //EasyExcel.write(out, ExcelPhysician.class).autoCloseStream(true).sheet("医师信息").doWrite(arr);
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8"));
            //
            EasyExcel.write(response.getOutputStream(), OrderSurfaceEntity.class).autoCloseStream(Boolean.FALSE).sheet("秒杀活动信息")
                    .doWrite(arr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //导出excel
//    @RequestMapping("/daochu")
//    public R Daochu(){
//        List<OrderSurfaceEntity> list = orderSurfaceService.list();
//        ExcelUtil<OrderSurfaceEntity> excelUtil = new ExcelUtil<>(OrderSurfaceEntity.class);
//        excelUtil.exportExcel(response,list,"活动秒杀订单");
//        return R.ok();
//    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Integer[] ids){
		orderSurfaceService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
